package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ExportDao;
import cn.itcast.dao.cargo.PackingDao;
import cn.itcast.domain.cargo.Export;
import cn.itcast.domain.cargo.ExportExample;
import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.PackingExample;
import cn.itcast.service.cargo.PackingService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Service(timeout = 100000)
public class PackingServiceImpl implements PackingService {
    @Autowired
    private ExportDao exportDao;

    @Autowired
    private PackingDao packingDao;

    @Override
    public Packing findById(String id) {
        return null;
    }

    @Override
    public void save(Packing packing) {
        //1. 设置装箱单id、商品数量、附件数量、状态
        packing.setPackingListId(UUID.randomUUID().toString());
        packing.setState(0);

        // 获取报运单id
        String exportIds = packing.getExportIds();
        String[] array = exportIds.split(",");
        List<String> exportIdList = Arrays.asList(array);

        //2. 根据购销合同id，获取合同号；修改购销合同状态为2表示已经生成报运
        ExportExample exportExample = new ExportExample();
        exportExample.createCriteria().andIdIn(exportIdList);
        List<Export> exportList = exportDao.selectByExample(exportExample);
        if (exportIdList == null || exportIdList.size() == 0){
            throw new RuntimeException("报运单id为空！");
        }
        // 保存合同号
        String exportNos = "";
        Double totalGrossWeight = 0d;
        Double totalNetWeight = 0d;
        Double totalVolume = 0d;
        Double packingMoney = 0d;
        // 遍历报运合同
        for (Export export : exportList) {
            exportNos = export.getCustomerContract() + " ";
            totalGrossWeight += export.getGrossWeights();
            totalVolume += export.getMeasurements();

        }
        // 设置合同号
        packing.setExportNos(exportNos);


        //5. 添加报运单（商品、附件已经添加）
        packingDao.insertSelective(packing);
    }

    @Override
    public void delete(String id) {

    }

    @Override
    public PageInfo<Packing> findByPage(PackingExample example, int pageNum, int pageSize) {
        return null;
    }
}
